<?php

namespace app\advertisement\logic;

use app\advertisement\model\ExpertManage as ExpertManageModel;
use think\facade\Db;
use app\data\util\HttpUtil;
use ba\Exception;
use think\facade\Cache;
use app\common\library\Excel;

class ExpertManageLogic
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new ExpertManageModel();
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $name = $params['name'] ?? '';
        $limit = $params['limit'] ?? 10;
        $where = [];

        if (!empty($name)) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }

        $list = $this->model
            ->where($where)
            ->order('id desc')
            ->paginate($limit);
        $lists = $list->items();

        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $name = $params['name'] ?? '';
        $where = [];

        if (!empty($name)) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }

        $lists = $this->model
            ->where($where)
            ->order('id desc')
            ->select()
            ->toarray();

        $header[] = ['网红名称', 'name'];
        $header[] = ['Tiktok ID','tiktok_id'];
        $header[] = ['主页链接','home_link'];
        $header[] = ['linktree链接','linktree_link'];
        $header[] = ['邮箱','Mailbox'];
        $header[] = ['whatsapp','whatsapp'];
        $header[] = ['店铺','shop'];
        $header[] = ['短视频内容方向','ShortVideoContentDirection'];
        $header[] = ['是否做直播领域(是/否)','IsItInTheFieldOfLiveStreaming'];
        $header[] = ['合作方式(免费/付费)','CollaborationMethod'];
        $header[] = ['总付费金额','TotalPaymentAmount'];
        $header[] = ['累计带货金额','AccumulatedAmountOfGoodsCarried'];
        $fileName = "expertManage" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }
    
    /**
     * @desc    导出下载模版
     * @date    2024/4/24
     * @author  luckyXiong
     */
    public function getExportInfo($params){
        $arr = [];
        $header[] = ['网红名称', 'name'];
        $header[] = ['Tiktok ID','tiktok_id'];
        $header[] = ['主页链接','home_link'];
        $header[] = ['linktree链接','linktree_link'];
        $header[] = ['邮箱','Mailbox'];
        $header[] = ['whatsapp','whatsapp'];
        $header[] = ['店铺','shop'];
        $header[] = ['短视频内容方向','ShortVideoContentDirection'];
        $header[] = ['是否做直播领域(是/否)','IsItInTheFieldOfLiveStreaming'];
        $header[] = ['合作方式(免费/付费)','CollaborationMethod'];
        $header[] = ['总付费金额','TotalPaymentAmount'];
        $header[] = ['累计带货金额','AccumulatedAmountOfGoodsCarried'];
        $fileName = "expertManage";
        return Excel::exportData($arr, $header, $fileName, 'xlsx');
    }

    /**
     * @desc    上传数据
     * @date    2024/4/24
     * @author  luckyXiong
     */
    public function getUploadInfo($url){
        $data         = Excel::getExcelData('.' . '/' . $url);
        $excelData    = isset($data[0]) ? $data[0] : $data;
        if(!$excelData)  return json(DefaultResponse(201, false, '暂无数据!', null));
        $excelData    = filterArr($excelData);
        foreach ($excelData as $value) {
            $name = isset($value['A'])&& $value['A'] ? str_replace("\t", "", $value['A']):'';
            $tiktok_id  = isset($value['B'])&& $value['B'] ? str_replace("\t", "", $value['B']):'';
            $home_link = isset($value['C'])&& $value['C'] ? str_replace("\t", "", $value['C']):'';
            $linktree_link = isset($value['C'])&& $value['D'] ? str_replace("\t", "", $value['D']):'';
            $Mailbox = isset($value['C'])&& $value['E'] ? str_replace("\t", "", $value['E']):'';
            $whatsapp = isset($value['C'])&& $value['F'] ? str_replace("\t", "", $value['F']):'';
            $shop = isset($value['C'])&& $value['G'] ? str_replace("\t", "", $value['G']):'';
            $ShortVideoContentDirection = isset($value['C'])&& $value['H'] ? str_replace("\t", "", $value['H']):'';
            $IsItInTheFieldOfLiveStreaming = isset($value['C'])&& $value['I'] ? str_replace("\t", "", $value['I']):'';
            $CollaborationMethod = isset($value['C'])&& $value['J'] ? str_replace("\t", "", $value['J']):'';
            $TotalPaymentAmount = isset($value['C'])&& $value['K'] ? str_replace("\t", "", $value['K']):'';
            $AccumulatedAmountOfGoodsCarried = isset($value['C'])&& $value['L'] ? str_replace("\t", "", $value['L']):'';
            
            if(!empty($tiktok_id)){
                $arr  = [
                    'name' => $name,
                    'tiktok_id' => $tiktok_id,
                    'home_link' => $home_link,
                    'linktree_link' => $linktree_link,
                    'Mailbox' => $Mailbox,
                    'whatsapp' => $whatsapp,
                    'shop' => $shop,
                    'ShortVideoContentDirection' => $ShortVideoContentDirection,
                    'IsItInTheFieldOfLiveStreaming' => $IsItInTheFieldOfLiveStreaming,
                    'CollaborationMethod' => $CollaborationMethod,
                    'TotalPaymentAmount' => $TotalPaymentAmount,
                    'AccumulatedAmountOfGoodsCarried' => $AccumulatedAmountOfGoodsCarried,
                ];
                $exist_info = $this->model
                    ->where('tiktok_id',$tiktok_id)
                    ->find();
                if($exist_info){
                    $this->model->where('id',$exist_info['id'])->update($arr);
                }else{
                    $this->model->insert($arr);
                }
            }
        }
        return  '导入成功';
    }

}
